package com.ruoyi.plus.pub.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.plus.pub.domain.PubConfigPlus;
import com.ruoyi.plus.pub.service.IPubConfigPlusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 系统配置Controller
 *
 */
@RestController
@RequestMapping("/pub/configPlus")
public class PubConfigPlusController extends BaseController {
    @Autowired
    private IPubConfigPlusService pubConfigService;

    /**
     * 查询系统配置列表
     */
    @PreAuthorize("@ss.hasPermi('pub:configPlus:list')")
    @GetMapping("/list")
    public TableDataInfo list(PubConfigPlus pubConfigPlus) {
        startPage();
        List<PubConfigPlus> list = pubConfigService.selectPubConfigList(pubConfigPlus);
        return getDataTable(list);
    }

    /**
     * 导出系统配置列表
     */
    @PreAuthorize("@ss.hasPermi('pub:configPlus:export')")
    @Log(title = "系统配置导出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PubConfigPlus pubConfigPlus) {
        List<PubConfigPlus> list = pubConfigService.selectPubConfigList(pubConfigPlus);
        ExcelUtil<PubConfigPlus> util = new ExcelUtil<>(PubConfigPlus.class);
        return util.exportExcel(list, "config");
    }

    /**
     * 获取系统配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('pub:configPlus:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pubConfigService.getById(id));
    }

    /**
     * 新增系统配置
     */
    @PreAuthorize("@ss.hasPermi('pub:configPlus:add')")
    @Log(title = "系统配置添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PubConfigPlus pubConfigPlus) {
        if (!pubConfigService.checkConfigKeyUnique(pubConfigPlus)) {
            return AjaxResult.error("新增参数'" + pubConfigPlus.getName() + "'失败，参数键名已存在");
        }
        return toAjax(pubConfigService.save(pubConfigPlus));
    }

    /**
     * 修改系统配置
     */
    @PreAuthorize("@ss.hasPermi('pub:configPlus:edit')")
    @Log(title = "系统配置修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PubConfigPlus pubConfigPlus) {
        if (!pubConfigService.checkConfigKeyUnique(pubConfigPlus)) {
            return AjaxResult.error("修改参数'" + pubConfigPlus.getName() + "'失败，参数键名已存在");
        }
        return toAjax(pubConfigService.updateById(pubConfigPlus));
    }

    /**
     * 删除系统配置
     */
    @PreAuthorize("@ss.hasPermi('pub:configPlus:remove')")
    @Log(title = "系统配置删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pubConfigService.removeByIds(Arrays.asList(ids)));
    }

    /* ---------------------------------------------------------------------------------------- */

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('pub:configPlus:edit')")
    @Log(title = "系统配置管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@Validated @RequestBody PubConfigPlus pubConfigPlus) {
        if (pubConfigPlus == null || 0 == pubConfigPlus.getId() || StringUtils.isEmpty(pubConfigPlus.getStatus())) {
            return AjaxResult.error("系统配置修改状态参数插入异常,修改失败!");
        }
        return toAjax(pubConfigService.updateById(pubConfigPlus));
    }
}
